//
//  FirstTableViewCell.m
//  DesighBook
//
//  Created by 梅明 on 15/9/14.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "FirstTableViewCell.h"
#import "DesighModel.h"
#import "UIImageView+AFNetworking.h"

@implementation FirstTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _count = 1;
    }
    return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


//创建第一个scrollview
-(void)createFirstScrollView:(NSArray *)firstDataArray
{
    _firstScrollView.contentSize = CGSizeMake(self.frame.size.width*3, 0);
    _firstScrollView.pagingEnabled = YES;
    _firstScrollView.showsHorizontalScrollIndicator = false;
    
    for (int i = 0; i < firstDataArray.count+1; i++) {
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, _firstScrollView.frame.size.height)];
        if (firstDataArray.count>0) {
            if (i<firstDataArray.count) {
                DesighModel *model = firstDataArray[i];
                [imgview setImageWithURL:[NSURL URLWithString:model.imgViewurl]];
            }
            else
            {
                DesighModel *model = firstDataArray[0];
                [imgview setImageWithURL:[NSURL URLWithString:model.imgViewurl]];
                
                //添加UIPagecontroll
                CGFloat height2 = _firstScrollView.frame.size.height - 30;
                _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, height2 , self.frame.size.width, 30)];
                _page.numberOfPages = 2;
                _page.backgroundColor = [UIColor clearColor];
                _page.pageIndicatorTintColor = [UIColor whiteColor];
                _page.currentPageIndicatorTintColor = [UIColor lightGrayColor];
                [_page addTarget:self action:@selector(imgChange:) forControlEvents:UIControlEventValueChanged];
                [self addSubview:_page];
            }
        }
       
        [_firstScrollView addSubview:imgview];
        
    }
    if (_timer.isValid) {
        [_timer invalidate];
        _timer = nil;
    }
   _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(getOut) userInfo:nil repeats:YES];

}

//timer的事件
-(void)getOut
{
    _count++;
    if (_count >=2) {
        _count =1;
        _firstScrollView.contentOffset = CGPointMake(0, 0);
        
    }
    [_firstScrollView setContentOffset:CGPointMake(_count*self.frame.size.width, 0) animated:YES];
    _page.currentPage = _count -1;
}

//pagecontroller的点击事件
-(void)imgChange:(UIPageControl *)page
{
    _firstScrollView.contentOffset = CGPointMake((_page.currentPage+1)*self.frame.size.width, 0);
}


//创建第二个scrollview
-(void)createSecondScrollView:(NSArray *)thirdDataArray
{
    _secondScrollView.showsHorizontalScrollIndicator = false;
    CGFloat heiht3 = self.frame.size.width*9/32;
    CGFloat width = heiht3*43/26;
    _secondScrollView.contentSize = CGSizeMake((width+8)*thirdDataArray.count-8, 0);

    for (int i = 0; i<thirdDataArray.count; i++) {
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake((width+8)*i, 0, width, heiht3)];
        DesighModel *model = thirdDataArray[i];
        [imgview setImageWithURL:[NSURL URLWithString:model.imgViewurl]];
        imgview.userInteractionEnabled = YES;
        imgview.tag = 100+i;
        //给图片添加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapevent:)];
        [imgview addGestureRecognizer:tap];
 
        //添加title1
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(width/2-50, 10, 100, 30)];
        label1.textAlignment = NSTextAlignmentCenter;
        label1.text = [NSString stringWithFormat:@"%@",model.imgtitle1];
        label1.textColor = [UIColor whiteColor];
        label1.shadowColor = [UIColor blackColor];
        label1.shadowOffset = CGSizeMake(1, 1);
        [imgview addSubview:label1];
        
        //添加中间横线
        UIImageView *imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(width/2-50, 40, 100, 5)];
        imgview1.image = [UIImage imageNamed:@"line_case@2x.png"];
        [imgview addSubview:imgview1];
        
        //添加title2
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(width/2-50, 45, 100, 20)];
        label2.textAlignment = NSTextAlignmentCenter;
        label2.text = model.imgtitle2;
        label2.textColor = [UIColor whiteColor];
        label2.font = [UIFont fontWithName:nil size:13];
        label2.shadowColor = [UIColor blackColor];
        label2.shadowOffset = CGSizeMake(1, 1);
        [imgview addSubview:label2];
        
        
        //添加UIPagecontroll
        CGFloat height2 = _firstScrollView.frame.size.height - 30;
        UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, height2 , self.frame.size.width, 30)];
        page.numberOfPages = 2;
        page.backgroundColor = [UIColor clearColor];
        page.pageIndicatorTintColor = [UIColor whiteColor];
        page.currentPageIndicatorTintColor = [UIColor lightGrayColor];
        
        
        [self addSubview:page];
        
        [_secondScrollView addSubview:imgview];
        
    }


}

-(void)tapevent:(UITapGestureRecognizer *)tap
{
    self.block(tap.view.tag);
}

- (void)startPlay:(BOOL)a{
    if (a) {
        
    }
}


@end
